package com.example.demo.common;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class FileSystemReader {

    public static String readFileFromSystem(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        if (!Files.exists(path)) {
            throw new FileNotFoundException("文件不存在: " + filePath);
        }

        String fileName = path.getFileName().toString();
        String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();

        try (InputStream inputStream = Files.newInputStream(path)) {
            switch (extension) {
                case "txt":
                    return FileReaderUtil.readTxtFile(inputStream);
                case "doc":
                    return FileReaderUtil.readDocFile(inputStream);
                case "docx":
                    return FileReaderUtil.readDocxFile(inputStream);
                default:
                    throw new IllegalArgumentException("不支持的文件格式: " + extension);
            }
        }
    }
}